﻿//'use strict';
/*userRunningRouteCtrl  Controllers */
//常跑路线
(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'app';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }
    module.controller('userRunningRouteCtrl', ['$scope', '$http', "$state", '$location', '$ngBootbox', 'dataService',
    function ($scope, $http, $state, $location, $ngBootbox, dataService) {
        var userId = $scope.userId = window.localStorage.id;
        $scope.carCode = (window.localStorage.carCode == "null") ? " " : window.localStorage.carCode;

        //copy  分割线
        //var carid = $scope.carid = window.localStorage.carId;
        //var carrierid = $scope.carrierid = "";
        var skip = $scope.skip = 0;
        var count = $scope.count = 10;

        //分页初始化
        var paginationOptions = {
            pageNumber: 0,
            pageSize: 10,
            currentPage: 1,
            sort: null
        };
        var routeList = $scope.routeList = {
            data: [],
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                            { displayName: "起始地", field: 'StartingPlaceName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            { displayName: "目的地", field: 'DestinationName', minWidth: 300, enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            //{ displayName: "生成日期", field: 'createTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                            {
                                displayName: "操作",
                                field: '操作',
                                cellFilter: 'checkmark',
                                cellTemplate: ' <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.routeDel(row.entity.Id)"></a>', enableColumnMenu: false
                            }],
            onRegisterApi: function (gridApi) {
                $scope.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                    paginationOptions.pageNumber = newPage * pageSize - pageSize;
                    paginationOptions.pageSize = pageSize;
                    paginationOptions.currentPage = newPage;
                    getDataList($scope.userId, parseInt(paginationOptions.pageNumber), paginationOptions.pageSize);
                });
            }
        };

        //初始化列表
        var getDataList = function (userId, skip, count) {
            dataService.GetUserCommonLines(userId, skip, count)
            //$http.get(Mustache.render("/api/" + "Line/GetUserCommonLines?userId={{userId}}&startIndex={{startIndex}}&count={{count}}", { userId: userId, startIndex: skip, count: count }))
            .then(function (res) {
                res = res.data.Response.Value||{List:[],Total:0};
                $scope.gridApi.grid.options.totalItems = res.Total;
                $scope.routeList.data = res.List;
                var currentPage = $scope.gridApi.grid.options.paginationCurrentPage;
                var totalPages = $scope.gridApi.pagination.getTotalPages();
                if (totalPages < currentPage) { $scope.gridApi.grid.options.paginationCurrentPage = 1; };
                $scope.seeks = false;
            }, function (rej) {
                $scope.seeks = false;
            });
        };
        getDataList(userId, skip, count);
        $scope.seeks = true;

        dataService.GetProvince()
        //$http.get(Mustache.render("/api/" + "Common/GetProvince"))
        .then(function (res) {
            res = res.data.Response;
            $scope.starProvinceList = res.List;
            $scope.endProvinceList = res.List;
        }, function (rej) {
        });

        //删除
        $scope.routeDel = function (id) {
            $ngBootbox.confirm('确认删除此线路？').then(function () {
                dataService.DeleteCommonLine(id)
                //$http.get(Mustache.render("/api/" + "Line/DeleteCommonLine?lineId={{lineId}}", { lineId: id }))
                .then(function (res) {
                    $scope.gridApi.pagination.raise.paginationChanged(paginationOptions.currentPage, paginationOptions.pageSize);
                }, function (rej) { });
            }, function () { return; });
        };


        $scope.starProvince = "";
        $scope.starProvinceList = "";

        $scope.starCity = "";
        $scope.starCityList = "";


        $scope.endProvince = "";
        $scope.endProvinceList = "";

        $scope.endCity = "";
        $scope.endCityList = "";


        $scope.changeStarProvince = function () {
            $scope.starCity = "";
            if ($scope.starProvince === null || $scope.starProvince == "" || $scope.starProvince == undefined) {
                $scope.starProvince = -1;
            }
            //$http.get(Mustache.render("/api/" + "Common/GetCity?provincecode={{provincecode}}", { provincecode: $scope.starProvince }))
            dataService.GetCity($scope.starProvince)
            .then(function (res) {
                res = res.data.Response;
                $scope.starCityList = res.List;
                $scope.areaList = "";
            }, function (rej) {
                console.error("areaApiService.provincecode error: ", rej);
            });
        };
        $scope.changeEndProvince = function () {
            $scope.endCity = "";
            if ($scope.endProvince === null || $scope.endProvince == "" || $scope.endProvince == undefined) {
                $scope.endProvince = -1;
            }
            //$http.get(Mustache.render("/api/" + "Common/GetCity?provincecode={{provincecode}}", { provincecode: $scope.endProvince }))
            dataService.GetCity($scope.endProvince)
            .then(function (res) {
                res = res.data.Response;
                $scope.endCityList = res.List;
            }, function (rej) {
                console.error("areaApiService.provincecode error: ", rej);
            });
        };

        //添加
        $scope.routeAdd = function () {
            $ngBootbox.confirm('确定保存该路线？').then(function () {
                if ($scope.gridApi.grid.options.totalItems >= 10) {
                    $ngBootbox.alert("最多添加10条路线");
                    return;
                } else {
                    dataService.AddUserCommonLine($scope.userId, $scope.starCity, $scope.endCity)
                    //$http.get(Mustache.render("/api/" + "Line/AddUserCommonLine?userId={{userId}}&startingPlaceCode={{startingPlaceCode}}&destinationCode={{destinationCode}}",
                    //{
                    //    userId: $scope.userId,
                    //    startingPlaceCode: $scope.starCity,
                    //    destinationCode:  $scope.endCity
                    //}))
                    .then(function (res) {
                        $scope.gridApi.pagination.raise.paginationChanged(paginationOptions.currentPage, paginationOptions.pageSize);
                    }, function (rej) { });
                }
            }, function () { return });

        }
    }]);
})();





